package com.action;

import org.springframework.beans.factory.annotation.Autowired;

import persist.user.Login;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.server.AlbumServer;
import com.server.ArticleServer;
import com.server.UserServer;
import com.tools.CacheMap;

@SuppressWarnings({ "serial", "unchecked" })
public class GoAction extends ActionSupport {

    private Integer uid;

    @Autowired
    ArticleServer articleServer;
    @Autowired
    AlbumServer albumServer;
    @Autowired
    UserServer userServer;

    public String execute() throws Exception {
        ActionContext act = ActionContext.getContext();
        act.put("LATEST_ARTICLE_EACH_TYPE", articleServer.getLatestArticleInEachType());
        act.put("ABOUT_ME", getLoginUser(uid));

        return SUCCESS;
    }

    private Login getLoginUser(Integer uid) {
        Login login = CacheMap.getUser(uid);
        if (null == login) {
            login = userServer.login(uid);
            CacheMap.putUser(login);
        }
        return login;
    }

    public String articleType() throws Exception {
        ActionContext act = ActionContext.getContext();
        act.getSession().put("ALL_ARTICLE_TYPE", articleServer.getAllArticleType());
        return SUCCESS;
    }

    public String eachImgType() throws Exception {
        ActionContext act = ActionContext.getContext();
        act.put("OLDEST_IMG_EACH_TYPE", albumServer.getOldestImgInEachType(0));
        return SUCCESS;
    }

    public String allImgType() throws Exception {
        ActionContext act = ActionContext.getContext();
        act.put("ALL_IMG_TYPE", albumServer.getAllImgType());
        return SUCCESS;
    }

    public String imgUpload() throws Exception {
        ActionContext act = ActionContext.getContext();
        act.getSession().put("ALL_IMG_TYPE", albumServer.getAllImgType());
        return SUCCESS;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

}
